package org.e_gold.solr.core.generation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.e_gold.util.StringUtil;

import static org.e_gold.util.FileUtil.writeFile;

public class GenerationController implements StaticData {

    static String sCurrentLine = "";

    protected void dataImportSites(List<Site> sites) {

        BufferedReader br = null;
        try {
            File template = new File(DATA_IMPORT_TEMPLATE);
            StringBuilder sb = new StringBuilder();
            for (Site site : sites) {
                if (!site.isCreated) {
                    br = new BufferedReader(new FileReader(template));
                    sb.append("\n");
                    while ((sCurrentLine = br.readLine()) != null) {
                        sb.append("\n" + sCurrentLine.replace("$site$", site.getName()));
                    }
                }
            }
            System.out.println(sb);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void coreImportGeneration(List<Site> sites) {

        try {
            candidateCoreImportGeneration(sites);
            jobCoreImportGeneration(sites);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void jobCoreImportGeneration(List<Site> sites) {

        System.out.println("jobCoreImportGeneration");
        try {
            File candidateTemplate = new File(JOB_TEMPLATE);
            BufferedReader br = null;
            if (sites != null && sites.size() > 0) {
                for (Site s : sites) {
                    if (!s.isCreated) {
                        StringBuilder jobSb = new StringBuilder();
                        br = new BufferedReader(new FileReader(candidateTemplate));
                        while ((sCurrentLine = br.readLine()) != null) {
                            jobSb.append("\n").append(sCurrentLine.replace("$SITE$", s.getName()).replace("$IP$", s.getIp()));
                        }
                        writeFile(jobSb.toString().substring(1), s.getName() + ".xml", JOB_DIRECTORY_PATH);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void candidateCoreImportGeneration(List<Site> sites) {

        System.out.println("candidateCoreImportGeneration");
        try {
            File candidateTemplate = new File(CANDIDATE_TEMPLATE);
            BufferedReader br = null;
            if (sites != null && sites.size() > 0) {
                for (Site s : sites) {
                    if (!s.isCreated) {
                        StringBuilder candidateSb = new StringBuilder();
                        br = new BufferedReader(new FileReader(candidateTemplate));
                        while ((sCurrentLine = br.readLine()) != null) {
                            candidateSb.append("\n").append(sCurrentLine.replace("$SITE$", s.getName()).replace("$IP$", s.getIp()));
                        }
                        writeFile(candidateSb.toString().substring(1), s.getName() + ".xml", CANDIDATE_DIRECTORY_PATH);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static class Site {

        private String name;
        private String ip;
        private Boolean isCreated;

        public Boolean getIsCreated() {

            return isCreated;
        }

        public void setIsCreated(Boolean isCreated) {

            this.isCreated = isCreated;
        }

        public Site(String profile) {

            if (!StringUtils.isEmpty(profile)) {
                String[] _s = profile.split(";");
                this.name = _s[0];
                this.ip = _s[1];
                this.isCreated = "1".equals(_s[2]);
            }
        }

        public Site(String name, String ip) {

            this.name = name;
            this.ip = ip;
        }

        public String getName() {

            return name;
        }

        public void setName(String name) {

            this.name = name;
        }

        public String getIp() {

            return ip;
        }

        public void setIp(String ip) {

            this.ip = ip;
        }

    }

    protected static List<Site> prepareSites() {

        List<Site> sites = new ArrayList<Site>();
        try {
            File listSite = new File(LIST_CORE_IMPORT_SITES);
            BufferedReader br = new BufferedReader(new FileReader(listSite));
            while ((sCurrentLine = br.readLine()) != null) {
                if (!StringUtils.isEmpty(sCurrentLine)) {
                    // Site s = new Site(sCurrentLine.split(";")[0], sCurrentLine.split(";")[1]);
                    Site s = new Site(sCurrentLine);
                    sites.add(s);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sites;
    }

    protected void customGeneration(List<Site> sites) {

        System.out.println("customGeneration");
        try {
            File custom = null;
            File customTemplate = new File(CUSTOM_TEMPLATE);
            StringBuilder customSb = null;
            BufferedReader br = null;
            String renameTo = StringUtil.getDate();
            for (Site s : sites) {
                if (!s.isCreated) {
                    String dir = StringUtil.getDir(s.getName());
                    custom = new File(PROFILE_DIRECTORY_PATH + dir + File.separator + "custom.properties");
                    // backup current custom.properties
                    custom.renameTo(new File(PROFILE_DIRECTORY_PATH + dir + File.separator + renameTo + "_custom.properties"));

                    customSb = new StringBuilder();
                    br = new BufferedReader(new FileReader(customTemplate));
                    while ((sCurrentLine = br.readLine()) != null) {
                        customSb.append("\n").append(sCurrentLine.replace("$SITE$", s.getName()).replace("$TABLEAU_SITE$", dir.toUpperCase().replace(".", "-")));
                    }
                    writeFile(customSb.toString().substring(1), "custom.properties", PROFILE_DIRECTORY_PATH + dir);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
